package com.shuai.controller;

import com.shuai.dto.ProjectDTO;
import com.shuai.dto.ProjectPageQueryDTO;
import com.shuai.entity.Project;
import com.shuai.result.PageResult;
import com.shuai.result.Result;
import com.shuai.service.ProjectService;
import com.shuai.vo.ProjectVO;
import com.shuai.vo.VisualProjectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/project")
@Slf4j
@Api(tags = "项目相关接口")
public class ProjectController {
    
    @Autowired
    private ProjectService projectService;

    /**
     * 创建项目
     * @Param projectDTO
     * @return Result
     * */
    @PostMapping()
    @ApiOperation("创建项目")
    public Result saveWithFileAndTeam(@RequestBody ProjectDTO projectDTO){
        log.info("创建项目，{}",projectDTO);
        projectDTO.getRelations().forEach(relation -> log.info("项目成员：{}",relation ));
        projectService.saveWithFileAndTeam(projectDTO);
        return Result.success();
    }

    /**
     *根据id查询项目信息
     * */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询项目信息")
    public Result<ProjectVO> getById(@PathVariable Long id){
        log.info("根据id查询项目信息：{}",id);
        ProjectVO projectVO = projectService.getById(id);
        return Result.success(projectVO);
    }

    /**
     * 修改项目信息
     * */
    @PutMapping()
    @ApiOperation("修改项目信息")
    public Result update(@RequestBody ProjectDTO projectDTO) {
        log.info("修改项目信息：{} , {}", projectDTO);
        projectDTO.getRelations().forEach(relation -> log.info("项目成员：{}",relation ));
        projectService.update(projectDTO);
        return Result.success();
    }

    /**
     * 企业内项目分页查询
     * @Param projectPageQueryDTO
     * @return Result<PageResult>
     * */
    @GetMapping("/page")
    @ApiOperation("分页查询项目")
    public Result<PageResult<Project>> page(ProjectPageQueryDTO projectPageQueryDTO){
        log.info("项目分页查询，参数为：{}",projectPageQueryDTO);
        PageResult<Project> pageResult   = projectService.pageQuery(projectPageQueryDTO);
        log.info("项目分页查询结果为：{}",pageResult);
        return Result.success(pageResult);
    }


    /**
     * 企业内项目分页查询
     * @Param projectPageQueryDTO
     * @return Result<PageResult>
     * */
    @GetMapping("/visualPage")
    @ApiOperation("分页查询项目")
    public Result<PageResult<VisualProjectVO>> visualPage(ProjectPageQueryDTO projectPageQueryDTO){
        log.info("项目分页查询，参数为：{}",projectPageQueryDTO);
        PageResult<VisualProjectVO> pageResult   = projectService.visualPageQuery(projectPageQueryDTO);
        log.info("项目分页查询结果为：{}",pageResult);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除项目")
    public Result delete(@PathVariable Long id){
        log.info("删除项目：{}",id);
        projectService.deleteWithFileAndTeamById(id);
        return Result.success();
    }
}
